//
//  ListDetailsTableViewCell.m
//  DolphinMusic
//
//  Created by dllo on 15/3/24.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ListDetailsTableViewCell.h"

@implementation ListDetailsTableViewCell

- (void)dealloc
{
    [_imageViewCollect release];
    [_song release];
    [_titleLabel release];
    [_authorLabel release];
    [_button release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.titleLabel];
        [_titleLabel release];
        
        self.authorLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.authorLabel];
        [_authorLabel release];
        
        self.button = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.contentView addSubview:self.button];
        
        self.imageViewCollect = [[UIImageView alloc] init];
        [self.contentView addSubview:self.imageViewCollect];
        [_imageViewCollect release];
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.titleLabel.frame = CGRectMake(10, 5, self.contentView.frame.size.width - 60, 30);
    self.titleLabel.font = [UIFont systemFontOfSize:15];
    self.titleLabel.text = self.song.title;
    
    self.authorLabel.frame = CGRectMake(10, 40, self.contentView.frame.size.width, 15);
    self.authorLabel.alpha = 0.4;
    self.authorLabel.font = [UIFont systemFontOfSize:13];
    self.authorLabel.text = self.song.author;
    
    self.button.frame = CGRectMake(self.contentView.frame.size.width - 50, 5, self.contentView.frame.size.height - 10, self.contentView.frame.size.height - 10);
    
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.imageViewCollect .frame = CGRectMake(self.contentView.frame.size.width - 35, 20, 20, 20);
    self.imageViewCollect.image = [UIImage imageNamed:@"weishoucang1.png"];
    
    
    DataBaseHandler *dbHandler = [DataBaseHandler shareInstance];
    NSMutableArray *arr = [dbHandler selectAllInSongCollection];
    

    
    for (Song *song in arr) {
        if ([self.song.song_id isEqualToString:song.song_id]) {
            self.ifCollect = YES;
            self.imageViewCollect.image = [UIImage imageNamed:@"yishoucang.png"];
        }
    }
}

- (void)buttonAction:(UIButton *)button
{
    DataBaseHandler *dbHandler = [DataBaseHandler shareInstance];
    self.ifCollect = !self.ifCollect;
    if (self.ifCollect == YES) {
        [dbHandler insertSong:self.song];
//        [self.button setBackgroundImage:[UIImage imageNamed:@"yishoucang.png"] forState:UIControlStateNormal];
        
        self.imageViewCollect.image = [UIImage imageNamed:@"yishoucang.png"];
    }
    else {
        [dbHandler deleteSong:self.song];
//        [self.button setBackgroundImage:[UIImage imageNamed:@"weishoucang1.png"] forState:UIControlStateNormal];
        self.imageViewCollect.image = [UIImage imageNamed:@"weishoucang1.png"];
    }
}





- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
